/**
 * Created by Administrator on 2017/8/21.
 */
/**
 * Created by mj on 2017/3/30.
 */
$(function () {
    //初始化用户列表
    $('#DeviceList').datagrid({
        url: "queryWithPaging",
        method: "post",
        title: "设备列表",
        striped: true,
        rownumbers: true,
        loadMsg: "努力加载中...",
        fit: true,
        fitColumns: true,//是否自动填充适合屏幕
        pagination: true,//是否显示底部分页工具栏
        pageSize: 20,
        pageList: [10, 15, 20, 25],
        showFooter: true,
        autoRowHeight: false,
        queryParams:{ s_deviceName:''},
        columns: [[
            {field: 'id', title: 'select', checkbox: true, align: 'center'},
            {field: 'deviceName', title: '设备名称', sortable: true, width: 150, align: "center"},
            {field: 'devicePosition', title: '摆放位置', sortable: true, width: 100, align: "center"},
            {
                field: 'deviceState',
                title: '状态',
                sortable: true,
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "<span style='color: yellow;'>空 闲</span>";
                    }
                    else if (value == 2) {
                        return "<span style='color: green;'>运 作</span>";
                    }
                    else if (value == 3) {
                        return "<span style='color:red;'>异 常</span>";
                    } else return "<span style='color: pink;'>维 修</span>";
                }
            }
        ]],
        toolbar: [
            {
                text: '增加', iconCls: 'icon-add', handler: function () {
                openAddWin();
            }
            }, '-',
            {
                text: '编辑', iconCls: 'icon-edit', handler: function () {
                editDevice();
            }
            }, '-',
            {
                text: '删除', iconCls: 'icon-remove', handler: function () {
                deleteDevice();
            }
            }, '-',

            {
                text: '搜索', iconCls: 'icon-search', handler: function () {
                openSearchWin();

            }
            }, '-',
            {
                text: '刷新', iconCls: 'icon-reload', handler: function () {
                $('#DeviceList').datagrid("reload");
            }
            },
            '-'],
    });
    $('#DeviceList').datagrid('getPager').pagination({//分页栏下方文字显示
        beforePageText: '第',
        afterPageText: '页，总共 {pages}页',
        displayMsg: '当前显示从第{from}条到{to}条 共{total}条记录',
    });
});
//打开添加设备窗口
function openAddWin() {
    openWin("DeviceList","addDeviceWin", "添加设备", "450px", "300px","addDeviceForm","save");
    $("#addDeviceWin").show().window("open");
}

function openSearchWin() {
    $("#searchDeviceWin").show().window("open");
}


//确认新添设备
function addDevice() {
    if ($("#addDeviceForm").form('validate')) {
        $.messager.confirm('确认', '您确认添加吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "POST",
                    url: "addMechanics",
                    data: $("#addDeviceForm").serialize(),
                    timeout: 20000,
                    dataType: "json",
                    cache: false,
                    success: function (data) {
                        if (data.msg == "success") {
                            $.messager.alert("提示", "添加成功！", "info", function () {
                                window.location.reload();
                            });
                        } else {
                            $.messager.alert("警告", "添加失败！", "info", function () {
                                window.location.reload();
                            })
                        }
                    },
                    error: function () {
                        $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
                    }
                })
            }
        });
    }
}

//确认搜索
function searchDevice(listId,fieldNameId) {
    $("#"+listId).datagrid('load', {
        fieldName:$("#"+fieldNameId).val()
    });
    $("#"+fieldNameId).val("");
    cancelSearch();
}
//取消添加
function cancelAdd() {
    //重置表单
    $("#addDeviceWin").window("close");
}

function cancelSearch() {
    //重置表单
    $("#s_deviceName").val("");
    $("#searchDeviceWin").window("close");
}

//删除，可批量删除
function deleteDevice() {
    var row = $("#DeviceList").datagrid("getSelections");//返回选中多行
    var ids = "";
    for (var i = 0; i < row.length; i++) {
        ids = ids + row[i].id + ",";
    }
    $.messager.confirm('确认', '您确认想要删除记录吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "delete",
                data: {ids: ids},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "删除成功！", "info", function () {
                            window.location.reload();
                        })
                    } else {
                        $.messager.alert("提示", "删除失败！", "info", function () {
                            window.location.reload();
                        });
                    }

                },
                error: function () {
                    $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
                }
            })
        }
    });
}
//修改信息
function editDevice() {
    if (!$('#DeviceList').datagrid('getSelected')) {
        return;
    }

    $("#id").val($.getSingleRecord("DeviceList").id);
    $("#deviceName").textbox("setValue", $.getSingleRecord("DeviceList").deviceName);
    $("#devicePosition").textbox("setValue", $.getSingleRecord("DeviceList").devicePosition);
    $("#deviceState").combobox("select",$.getSingleRecord("DeviceList").deviceState);
    openWin("DeviceList","addDeviceWin", "修改设备","450px", "300px","addDeviceForm","update")
    $("#addDeviceWin").show().window("open");


}

